//
//  QAddCommonVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/19.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QAddCommonVC.h"
#import "QAddCommonCell.h"
#import "QAddressCell.h"
#import "QDetailAddressCell.h"
#import "QCommonStateVC.h"
#import "QMapLocationVC.h"

@interface QAddCommonVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString *_allStr;//省市区
    NSString *_nameStr;
    NSString *_phoneStr;
    NSString *_detailStr;
    QInvoiceListModel *_model;

}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSArray *titleArr;

@end

@implementation QAddCommonVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加发票";
    [self setupForDismissKeyboard];

    _titleArr =[NSArray arrayWithObjects:@"发票类型",@"收票人姓名",@"收票人手机",@"",nil];
    _allStr =@"";
    _nameStr =@"";
    _phoneStr=@"";
    _detailStr=@"";
    _model =[QInvoiceListModel new];
    //普票
    [self createUI];
}

-(void)createUI{
    [self.view addSubview:self.tbView];
    
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    UIView *header =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    header.backgroundColor = [UIColor clearColor];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, kScreenWidth, 40)];
    titleLab.font = [UIFont systemFontOfSize:13];
    titleLab.textColor = [QColorUtility defaultDeepGrayColor];
    titleLab.text = @"为保障正常开票，请准确填写以下信息";
    [header addSubview:titleLab];
    self.tbView.tableHeaderView = header;
    
    //提交
    UIView *footer =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 80)];
    
    UIButton * submitBtn = [QViewUtility createButtonWithFrame:CGRectMake(40, 40, kScreenWidth - 80, 45) font:Ksize17 title:@"提 交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    submitBtn.layer.cornerRadius =7;
    submitBtn.layer.masksToBounds = YES;
    
    [submitBtn addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn blueStyle];
    
    [footer addSubview:submitBtn];
    self.tbView.tableFooterView = footer;
    
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QAddCommonCell" bundle:nil] forCellReuseIdentifier:@"QAddCommonCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QAddressCell" bundle:nil] forCellReuseIdentifier:@"QAddressCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QDetailAddressCell" bundle:nil] forCellReuseIdentifier:@"QDetailAddressCell"];
    
}
#pragma mark ----提交
- (void)submitAction{
    if ([_nameStr isEqualToString:@""]) {
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写姓名" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }else if (_phoneStr.length!=11){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确电话" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }else if ([_allStr isEqualToString:@""]){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写正确地址" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }else if ([_detailStr isEqualToString:@""]){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请填写详细地址" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }
    
    NSString *url =[QParamService urlWithAction:@"createCredentials"];
    NSDictionary *param =[QParamService createCredentialsWithInvoiceType:@"1" Name:_nameStr Tel:_phoneStr Address:[NSString stringWithFormat:@"%@%@",_allStr,_detailStr]];
    
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            //提交

            _model.cusType =@"1";
            _model.receiveName =_nameStr;
            _model.receivePhone =_phoneStr;
            _model.invoiceType =@"1";
            _model.state =@"1";
            _model.receiveAddress =[NSString stringWithFormat:@"%@%@",_allStr,_detailStr];
            
            QCommonStateVC * vc =[[QCommonStateVC alloc]init];
            vc.model =_model;
            vc.pop =YES;
            vc.pushFromWhere = @"QAddCommonVC";
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        
        WS(wSelf);
        
        QAddressCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QAddressCell"];
        __weak QAddressCell * wCell = cell;
        if ([_allStr isEqualToString:@""]) {
            cell.placeLab.text =@"定位到省 市 区";
        }else{
            cell.placeLab.text =_allStr;
        }
        cell.MapBlock=^(){
            QMapLocationVC *vc = [[QMapLocationVC alloc] init];
            vc.block = ^(double lat,double lon,NSString *str,NSString *subStr,NSString *allStr){
                CLog(@"%@===%@",subStr,allStr);
                _allStr=allStr;
                wCell.placeLab.text = _allStr;
            };
            [wSelf.navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }else if (indexPath.section == 4){
        QDetailAddressCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QDetailAddressCell"];
        cell.detailTf.text =_detailStr;
        [cell.detailTf addTarget:self action:@selector(detailAction:) forControlEvents:UIControlEventEditingChanged];
        return cell;
    }else{
        QAddCommonCell *cell =[tableView dequeueReusableCellWithIdentifier:@"QAddCommonCell"];
        cell.textTF.tag =indexPath.section;
        [cell.textTF addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventEditingChanged];
        cell.titleLab.text =_titleArr[indexPath.section];
        
        if (indexPath.section == 0) {
            cell.textTF.hidden = YES;
            cell.invoiceTypeLab.hidden = NO;
            cell.invoiceTypeLab.text =@"普票";

        }else if (indexPath.section ==1){
            cell.textTF.hidden = NO;
            cell.invoiceTypeLab.hidden = YES;
            cell.textTF.placeholder =@"请输入收票人姓名";
            cell.textTF.text = _nameStr;

        }else if (indexPath.section ==2){
            cell.textTF.hidden = NO;
            cell.invoiceTypeLab.hidden = YES;
            cell.textTF.placeholder =@"请输入收票人手机";
            cell.textTF.text = _phoneStr;
            cell.textTF.keyboardType = UIKeyboardTypeNumberPad;

        }
        return cell;
    }
}
- (void)textFieldAction:(UITextField *)textField{
    if (textField.tag == 1) {
        _nameStr = textField.text;
    }else if (textField.tag == 2){
        _phoneStr = textField.text;
    }
}

- (void)detailAction:(UITextField *)textField  {
    _detailStr =textField.text;
}
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
